import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import setpage from '../page/setpage/index.vue'
import effect from '../page/effect/index.vue'
import home from '../page/home/index.vue'
import ble from '../page/ble/index.vue'
import { blestore } from '../store/ble'



const routes: Array<RouteRecordRaw> = [
  // ...
  {
    path: "/",
    name: "index",
    component: home
  },
  {
    path: "/set",
    name: "set",
    component: setpage
  },
  {
    path: "/effect",
    name: "effect",
    component: effect
  },
  {
    path: "/ble",
    name: "ble",
    component: ble
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})
// ---------------------- 路由拦截 方法 -----------------------------------//

router.beforeEach((to, from, next) => {
  const ble = blestore()
  //是否连接蓝牙
  const isConnectBLE: boolean = ble.isConnectBLE

  // document.title = `${to.meta.title} | vue-manage-system`;
  //   const role = localStorage.getItem('ms_username');
  if (!isConnectBLE && to.path !== '/ble') {
    console.log(to.fullPath )
    next({
      path: '/ble',
      query: { redirect: to.fullPath }
    });
  } else {
    if (isConnectBLE && to.path === '/ble') {
      next();
    }
    next();
  }
})
export default router
